<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用 FreeType 2 字体将文本写入图像</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.imagefttext" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagefttext</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.7, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">imagefttext</span> &mdash; <span class="dc-title">使用 FreeType 2 字体将文本写入图像</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.imagefttext-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imagefttext</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span>
   , <span class="methodparam"><span class="type">float</span> <code class="parameter">$size</code></span>
   , <span class="methodparam"><span class="type">float</span> <code class="parameter">$angle</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$x</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$y</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$color</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$fontfile</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$text</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$extrainfo</code></span>
  ] ) : <span class="type">array</span></div>

 </div>

 <div class="refsect1 parameters" id="refsect1-function.imagefttext-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<code class="parameter">image</code></dt>

<dd>
<p class="para">由图象创建函数(例如<span class="function"><a href="imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>)返回的图象资源。</p></dd>

    
     <dt>
<code class="parameter">size</code></dt>

     <dd>

      <p class="para">
       The font size to use in points.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">angle</code></dt>

     <dd>

      <p class="para"> 
       The angle in degrees, with 0 degrees being left-to-right reading text.
       Higher values represent a counter-clockwise rotation. For example, a 
       value of 90 would result in bottom-to-top reading text.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">x</code></dt>

     <dd>

      <p class="para">
       The coordinates given by <code class="parameter">x</code> and
       <code class="parameter">y</code> will define the basepoint of the first
       character (roughly the lower-left corner of the character). This
       is different from the <span class="function"><a href="imagestring.html" class="function">imagestring()</a></span>, where
       <code class="parameter">x</code> and <code class="parameter">y</code> define the
       upper-left corner of the first character. For example, &quot;top left&quot;
       is 0, 0.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">y</code></dt>

     <dd>

      <p class="para">
       The y-ordinate. This sets the position of the fonts baseline, not the
       very bottom of the character.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">color</code></dt>

     <dd>

      <p class="para">
       The index of the desired color for the text, see 
       <span class="function"><a href="imagecolorexact.html" class="function">imagecolorexact()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">fontfile</code></dt>

     <dd>

      <p class="para">
       The path to the TrueType font you wish to use.
      </p>
      <p class="para">
       Depending on which version of the GD library PHP is using, <em class="emphasis">when
       <code class="parameter">fontfile</code> does not begin with a leading
       <em>/</em> then <em>.ttf</em> will be appended</em>
       to the filename and the library will attempt to search for that
       filename along a library-defined font path.
      </p>
      <p class="para">
       When using versions of the GD library lower than 2.0.18, a <em>space</em> character,
       rather than a semicolon, was used as the &#039;path separator&#039; for different font files.
       Unintentional use of this feature will result in the warning message:
       <em>Warning: Could not find/open font</em>. For these affected versions, the
       only solution is moving the font to a path which does not contain spaces.
      </p>
      <p class="para">
       In many cases where a font resides in the same directory as the script using it
       the following trick will alleviate any include problems.
       <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;the&nbsp;enviroment&nbsp;variable&nbsp;for&nbsp;GD<br /></span><span style="color: #0000BB">putenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'GDFONTPATH='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">realpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Name&nbsp;the&nbsp;font&nbsp;to&nbsp;be&nbsp;used&nbsp;(note&nbsp;the&nbsp;lack&nbsp;of&nbsp;the&nbsp;.ttf&nbsp;extension)<br /></span><span style="color: #0000BB">$font&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SomeFont'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
       </div>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">text</code></dt>

     <dd>

      <p class="para">
       Text to be inserted into image. 
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">extrainfo</code></dt>

     <dd>

      <p class="para">
       <table class="doctable table">
       <caption><strong>Possible array indexes for <code class="parameter">extrainfo</code></strong></caption>
        
         <thead>
          <tr>
           <th>Key</th>
           <th>Type</th>
           <th>Meaning</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><em>linespacing</em></td>
           <td><span class="type"><a href="language.types.float.html" class="type float">float</a></span></td>
           <td>Defines drawing linespacing</td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.imagefttext-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   This function returns an array defining the four points of the box, starting in the lower left and moving counter-clockwise:
   <table class="doctable informaltable">
    
     <tbody class="tbody">
      <tr>
       <td>0</td>
       <td>lower left x-coordinate</td>
      </tr>

      <tr>
       <td>1</td>
       <td>lower left y-coordinate</td>
      </tr>

      <tr>
       <td>2</td>
       <td>lower right x-coordinate</td>
      </tr>

      <tr>
       <td>3</td>
       <td>lower right y-coordinate</td>
      </tr>

      <tr>
       <td>4</td>
       <td>upper right x-coordinate</td>
      </tr>

      <tr>
       <td>5</td>
       <td>upper right y-coordinate</td>
      </tr>

      <tr>
       <td>6</td>
       <td>upper left x-coordinate</td>
      </tr>

      <tr>
       <td>7</td>
       <td>upper left y-coordinate</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.imagefttext-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3708">
    <p><strong>Example #1 <span class="function"><strong>imagefttext()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;300x100&nbsp;image<br /></span><span style="color: #0000BB">$im&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">300</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$red&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0xFF</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$black&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Make&nbsp;the&nbsp;background&nbsp;red<br /></span><span style="color: #0000BB">imagefilledrectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">299</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">99</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$red</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Path&nbsp;to&nbsp;our&nbsp;ttf&nbsp;font&nbsp;file<br /></span><span style="color: #0000BB">$font_file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'./arial.ttf'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Draw&nbsp;the&nbsp;text&nbsp;'PHP&nbsp;Manual'&nbsp;using&nbsp;font&nbsp;size&nbsp;13<br /></span><span style="color: #0000BB">imagefttext</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">105</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">55</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$black</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$font_file</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP&nbsp;Manual'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Output&nbsp;image&nbsp;to&nbsp;the&nbsp;browser<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;image/png'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">imagepng</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.imagefttext-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数仅在
PHP 编译时加入 freetype 支持时有效（<strong class="option configure">--with-freetype-dir=DIR</strong>
）。</span></p></blockquote>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.imagefttext-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.5</td>
       <td>
        <code class="parameter">extrainfo</code> was made optional.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

</div></div></div></body></html>